package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.DealHistoryDetailListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.ui.view.MyListView;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDePeddingCommentFragment extends BaseFragment {

	private static WeDePeddingCommentFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;


	private List<DealHistoryDetailObject> dealHistoryListObjects = new ArrayList<DealHistoryDetailObject>();
	private List<DealHistoryDetailObject> completeHistoryListObjects = new ArrayList<DealHistoryDetailObject>();
	private int pageNo = 0;
	private DealHistoryDetailListAdapter dealHistoryDetailListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_padding_comment, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_pedding_comment));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);
		MyListView myListView = new MyListView(mBaseActivity);
		listView.addHeaderView(myListView);
		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
			}
		});
	}

	public void onResume() {
		super.onResume();
		msgListRefreshLayout.autoRefresh();
	}

	public void initData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
//		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		//0 表示未评价  1 表示已经评价
		paramMap.put("type", "0");
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_DEALS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						DealHistoryDetailListObjectResponce rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), DealHistoryDetailListObjectResponce.class);
						if (pageNo == 0) {
							dealHistoryListObjects.clear();
						}
						dealHistoryListObjects.addAll(rep.getData());
						Log.d("aa", dealHistoryListObjects + "");
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
		//initAlreadyCommentData();
	}
	//已经评价的订单
	public void initAlreadyCommentData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
//		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		//0 表示未评价  1 表示已经评价
		paramMap.put("type", "1");
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_DEALS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						DealHistoryDetailListObjectResponce rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), DealHistoryDetailListObjectResponce.class);

						dealHistoryListObjects.addAll(rep.getData());

						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}


	private void initListView() {
		if (dealHistoryDetailListAdapter == null) {
			//筛选已完成状态的订单
			 completeHistoryListObjects = new ArrayList<DealHistoryDetailObject>();
			for(int i=0;i<dealHistoryListObjects.size();i++){
				if(dealHistoryListObjects.get(i).getStatus().equals("4")){
					completeHistoryListObjects.add(dealHistoryListObjects.get(i));
				}
			}
			Log.d("aa",completeHistoryListObjects.size()+"");
			Log.d("aa",completeHistoryListObjects+"");
			if (completeHistoryListObjects.size()==0){

				TextView emptyView = new TextView(mBaseActivity);
				emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
				emptyView.setText("暂无待评价订单");
				emptyView.setVisibility(View.GONE);
				((ViewGroup) listView.getParent()).addView(emptyView);
				listView.setEmptyView(emptyView);
			}else {

			dealHistoryDetailListAdapter = new DealHistoryDetailListAdapter(mBaseActivity, completeHistoryListObjects);
			listView.setAdapter(dealHistoryDetailListAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					WeDePeddingCommentAddFragment.setDealHistoryDetailObject(completeHistoryListObjects.get(i));
					CommUtil.openSingleFragmentActivity(mBaseActivity, WeDePeddingCommentAddFragment.class);
				}
			});
			}
		}



		dealHistoryDetailListAdapter.notifyDataSetChanged();
	}


	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDePeddingCommentFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
